<template>
  <div class="login-wrap" style="height: 100vh;display: flex; align-items: center;justify-content: center;">
    <div style="display: flex; background-color: white; box-sizing: border-box;width: 25%; border-radius: 5px; overflow: hidden">
      <div style="flex: 1; display: flex; align-items: center; justify-content: center;background-color: #ffffff">
        <el-form :model="formData" style="width: 80%" :rules="rules" ref="form">
          <div style="font-size: 28px;  margin-top: 30px; text-align: center; margin-bottom: 30px;color: #000000">
            课 程 管 理 系 统
          </div>
          <el-form-item prop="username">
            <!--必须绑定v-model输入框才能输入字符---->
            <el-input v-model="formData.username" placeholder="学号/工号/用户名" autocomplete="off">
              <i slot="prefix" class="el-input__icon el-icon-user"></i>
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input v-model="formData.password" size="medium" placeholder="请输入密码" clearable show-password autocomplete="off">
              <i slot="prefix" class="el-input__icon el-icon-lock"></i>
            </el-input>
          </el-form-item>
          <el-form-item prop="code">
            <div style="display: flex">
              <el-input style="flex: 1" size="medium" v-model="code"></el-input>
              <Identify :identifyCode="identifyCode" @click.native="refreshCode" />
            </div>
          </el-form-item>
          <el-form-item prop="userType" >
            <el-radio-group v-model="formData.userType" label-width="100%" >
              <el-radio-button label="1" style="color: #7ab3bd">学生</el-radio-button>
              <el-radio-button label="2" style="color: #7ab3bd">教师</el-radio-button>
              <el-radio-button label="3" style="color: #7ab3bd">教务管理员</el-radio-button>
            </el-radio-group>
          </el-form-item>

          <!--          <el-form-item prop="userType">-->
          <!--            <el-radio-group v-model="formData.userType">-->
          <!--              <el-radio label="1">学生</el-radio>-->
          <!--              <el-radio label="2">教师</el-radio>-->
          <!--              <el-radio label="3">教务管理员</el-radio>-->
          <!--            </el-radio-group>-->
          <!--          </el-form-item>-->
          <el-form-item style="color: #000000">
            <el-button type="primary" style="width: 100%;background-color: #33929c" @click="submit()">登 录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { login } from "../api/user";
import Identify from "@/components/Identify";

export default {
  components: {
    Identify
  },
  data: function() {
    return {
      formData: {
        username: "",
        password: "",
        userType: "1",
      },
      code: '',   // 表单绑定的验证码
      // 图片验证码
      identifyCode: '',
      // 验证码规则
      identifyCodes: '123456789ABCDEFGHGKMNPQRSTUVWXY',
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        userType: [
          { required: true, message: "请选择用户类型", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.refreshCode()
  },
  methods: {
    // 切换验证码
    refreshCode() {
      this.identifyCode = ''
      this.makeCode(this.identifyCodes, 4)
    },
    // 生成随机验证码
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode += this.identifyCodes[Math.floor(Math.random() * (this.identifyCodes.length))]
      }
    },
    submit() {
      if (!this.code) {
        this.$message.warning('请输入验证码')
        this.refreshCode()
        return
      }
      if (this.code.toLowerCase() !== this.identifyCode.toLowerCase()) {
        this.$message.warning('验证码错误')
        this.refreshCode()
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          login(
            this.formData.username,
            this.formData.password,
            this.formData.userType
          ).then((res) => {
            this.$message.success("登录成功: " + res.username);
            this.$store.commit("login", res);
            this.$router.push({ name: "container" });
          });
        }
      });
    },
  },
};
</script>

<style scoped>



.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  /* background: #0f9876; */
  /*background-color: #5a83c9;*/
  background-image: url("../assets/bg1.jpg");
  background-size: 100% 100%;
}

.form-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #fff;
  border-bottom: 1px solid #ddd;
}

.login-form {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 350px;
  margin: -190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(0, 0, 0, 0.6);
  overflow: hidden;
}

.form-content {
  padding: 30px 30px;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 100%;
  height: 36px;
}

.el-radio {
  color: #fff;
}
</style>
